﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.IO;

public class StudentManager
{
    private List<Student> students;

    public StudentManager()
    {
        students = new List<Student>();
    }

    // 1. 增加一个学生
    public bool AddStudent(Student student)
    {
        if (students.Any(s => s.StudentId == student.StudentId))
        {
            Console.WriteLine($"错误：学号 {student.StudentId} 已存在！");
            return false;
        }

        students.Add(student);
        Console.WriteLine($"成功添加学生：{student.Name}");
        return true;
    }

    // 2. 修改一个指定学号的学生
    public bool UpdateStudent(string studentId, string name, int age, string major, string className)
    {
        Student student = FindStudentById(studentId);
        if (student != null)
        {
            student.Name = name;
            student.Age = age;
            student.Major = major;
            student.Class = className;
            Console.WriteLine($"成功修改学生信息：{student}");
            return true;
        }
        else
        {
            Console.WriteLine($"错误：找不到学号为 {studentId} 的学生！");
            return false;
        }
    }

    // 3. 删除一个学生
    public bool DeleteStudent(string studentId)
    {
        Student student = FindStudentById(studentId);
        if (student != null)
        {
            students.Remove(student);
            Console.WriteLine($"成功删除学生：{student.Name}");
            return true;
        }
        else
        {
            Console.WriteLine($"错误：找不到学号为 {studentId} 的学生！");
            return false;
        }
    }

    // 4. 查询一个指定学号的学生
    public Student FindStudentById(string studentId)
    {
        return students.FirstOrDefault(s => s.StudentId == studentId);
    }

    // 5. 遍历所有学生
    public void DisplayAllStudents()
    {
        if (students.Count == 0)
        {
            Console.WriteLine("当前没有学生信息！");
            return;
        }

        Console.WriteLine($"\n共有 {students.Count} 名学生：");
        Console.WriteLine("==========================================");
        for (int i = 0; i < students.Count; i++)
        {
            Console.WriteLine($"{i + 1}. {students[i]}");
        }
        Console.WriteLine("==========================================");
    }

    // 获取学生数量
    public int GetStudentCount()
    {
        return students.Count;
    }

    // 获取学生列表（用于序列化）
    public List<Student> GetStudents()
    {
        return students;
    }

    // 设置学生列表（用于反序列化）
    public void SetStudents(List<Student> studentList)
    {
        students = studentList;
    }
}